#!/usr/bin/perl

$RUN_FILE   = "RUNcleans.run";
$PHASE_FILE = "RUNphases.run";
$OUTPUT     = "OUTphases.dat";

##################### TIME SERIES OPTIONS #####################
$start = $ARGV[0];
$end = $ARGV[1];
@lengths = (3000);
###############################################################

##################### PARAMATER FILE OPTIONS ###################
$Ncomponents = 8;
$sampling = "E";
#         1     2    3     4    5    6    7    8    9    10
@Tamps = (1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
@Tfreqs = ($ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7],
	   $ARGV[8], $ARGV[9], $ARGV[10]);
@Tphase = (0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2);
################################################################
print "start: $start\n";
print "end: $end\n";
print "freqs: ";
foreach (@Tfreqs) {
    print " $_ ";
}
print "\n";

open(RUN,">$RUN_FILE");
open(PHASE,">$PHASE_FILE");
print PHASE "> $OUTPUT\n";

foreach $length (@lengths) {
    # ================== check what we are generating is sensible ====================
    $delta = ($end-$start)/$length;
    $max_freq = 1/($delta*2);
    $res = 1/($end - $start);
    $spacing = 1/(4*($end - $start));
    if ($max_freq < 10) { 
	print "ERR: max freq $max_freq, not big enough => length too short or period too long\n";
	exit(1);
    }
     if ($res > 0.3) { 
	 print "ERR: resolution $res not enough to separate some of the frequencies\n";
	 exit(1);
    }
    # ================= make the paramter file =======================================
    $PARM_FILE = "data/OFFSET_${length}.parm";
    $DAT_FILE =  "data/OFFSET_${length}.dat";
    
    open(PARM,">$PARM_FILE");
    print PARM "$length     \n";
    print PARM "$start $end  \n";
    print PARM "$sampling  \n";
    print PARM "$Ncomponents \n";
    for ($i=0;$i<$Ncomponents;$i++) {
	print PARM "$Tamps[$i] $Tfreqs[$i] $Tphase[$i]  \n";
    }
    close(PARM);
    
    print "\n";
    print "> Generating parameter file\n";
    print "> --------------------------- \n";
    print "> Length           = $length\n"; 
    print "> Time range       = ($start to $end)\n";
    print "> Max freq.        = $max_freq\n";
    print "> Freq. resolution = $res\n";
    print "> Delta spacing    = $delta $spacing\n";
    print "> --------------------------- \n";
    print "> CLEAN run file   = $RUN_FILE \n";
    print "> PHASE run file   = $PHASE_FILE \n";
    print "> PHASE outputs    = $OUTPUT \n";
    print "> --------------------------- \n";
    print "\n";
    # ================================================================================
    
    ## make the data files from parameter files generated
    system("perl data/data_gen_times.pl $PARM_FILE > data/out.$length.log");
    
    ## make the run cleans file
    print RUN "./cleanPP -i=$DAT_FILE -o=spectra/OFFSET_$length\n";
    
    ## make the get phase file
    for ($i=0;$i<$Ncomponents;$i++) {
	print PHASE "perl GETphases.pl $Tfreqs[$i] spectra/OFFSET_${length}/ ";
	print PHASE "$length $start $end >> $OUTPUT\n";
    }   
} # end loops over lengths
close(RUN);
close(PHASE);

# set the file permisions on run files
system("chmod u+x $RUN_FILE");
system("chmod u+x $PHASE_FILE");

system("./$RUN_FILE");
system("./$PHASE_FILE");
